<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-13 20:39:28
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-05-26 23:49:06
*/
Class GoodsTypeModel extends Model{
	public $table = 'goods_type';
	//添加商品类型
	function addGoodsType($data){
		if(empty($data)){
			$this->error='没有数据传输!';
			return false;
		}
		$this->validate=array(
			array('gtname','nonull','类型名不能为空',2,1)
		);
		if($this->create($data)){
			if(M('goods_type')->where(array('gtname'=>$data['gtname']))->find()){
				$this->error='类型已经存在';
				return false;
			}
			$s = $this->add($data);
			if($s){
				if($this->updateCache()){
					return $s;
				}
			}
			return false;
		}else{
			return false;
		}
	}
	//修改商品类型
	function saveGoodsType($data){
		if(empty($data)){
			$this->error='没有数据传输!';
			return false;
		}
		$this->validate=array(
			array('gtname','nonull','类型名不能为空',2,1)
		);
		if($this->create($data)){
			$s = $this->save($data);
			if($s){
				if($this->updateCache()){
					return $s;
				}
			}
			return false;
		}else{
			return false;
		}
	}
	//删除商品类型
	function delGoodsType($id){
		$s = $this->where(array('gtid'=>$id))->del();
		if($s){
			if($this->updateCache()){
				return $s;
			}
		}
		return false;
	}

	//添加商品,获得属性表单=================
	public function get($gtid,$data=null){
		$aclass = M('attr_class')->where(array('gtid'=>$gtid,'is_spec'=>0))->all();
		if($aclass){
			foreach ($aclass as $n => $v) {
				if(method_exists($this, $v['show_type'])){
					$field =M('attr_value')->where(array('acid'=>$v['acid']))->all();
					$aclass[$n]['html'] = $this->$v['show_type']($field,$data);
				}
			}
		}
		return $aclass;
	}

	//执行商品类型的函数
	private function input($fields,$data){
		if($data){
			$v = $data[$fields[0]['avid']]['avalue'];
		}else{
			$v=$fields[0]['attr_value'];
		}
		$html = '';
		$html.='<input name="attr['.$fields[0]['acid'].'|'.$fields[0]['avid'].']" type="text" value="'.$v.'">';
		return $html;
	}
	private function selected($fields,$data){
		$html = '<select name="attr['.$fields[0]['acid'].']">';
		foreach ($fields as $v) {
			// if($data){
				$sel = isset($data[$v['avid']])? 'selected=""' : '';
			// }
			$html.='<option '.$sel.' value="'.$v['avid'].'|'.$v['attr_value'].'">'.$v['attr_value'].'</option>';
		}
		$html.='</select>';
		return $html;
	}
	private function radio($fields,$data){
		$html = '';
		$i=0;
		foreach ($fields as $v) {
			// if($data){
				$sel = isset($data[$v['avid']])? 'checked=""' : '';
			// }
			if($i){
				$html.='<label class="mr10" for="'.$v['avid'].'"> <input '.$sel.' id="'.$v['avid'].'" name="attr['.$v['acid'].']" type="radio" value="'.$v['avid'].'|'.$v['attr_value'].'"> '.$v['attr_value'].'</label>';
			}else{
				$html.='<label class="mr10" for="'.$v['avid'].'"> <input '.$sel.' id="'.$v['avid'].'" name="attr['.$v['acid'].']" type="radio" checked="" value="'.$v['avid'].'|'.$v['attr_value'].'"> '.$v['attr_value'].'</label>';
			}
			$i++;
		}
		return $html;
	}
	private function checkbox($fields,$data){
		$html = '';
		foreach ($fields as $v) {
			// if($data){
				$sel = isset($data[$v['avid']])? 'checked=""' : '';
			// }
			$html.='<label class="mr10" for="'.$v['avid'].'"> <input '.$sel.' id="'.$v['avid'].'" name="attr['.$v['avid'].']" type="checkbox" value="'.$v['avid'].'|'.$v['attr_value'].'" /> '.$v['attr_value'].'</label>';
		}
		return $html;
	}
	private function textarea($fields,$data){
		if($data){
			$v = $data[$fields[0]['avid']]['avalue'];
		}else{
			$v=$fields[0]['attr_value'];
		}
		$html = '';
		$html.='<textarea class="pct50" name="attr['.$fields[0]['acid'].'|'.$fields[0]['avid'].']">'.$v.'</textarea>';
		return $html;
	}









	//更新缓存
	public function updateCache(){
		$goodsTypeDate = $this->all();
		$cacheData = array();
		foreach ($goodsTypeDate as $v) {
			$cacheData[$v['gtid']] = $v;
		}
		return cache('goodsType',$cacheData);
	}
}